package mudfox.controller;

import io.swagger.annotations.ApiOperation;

import mudfox.entity.Result;
import mudfox.entity.User;
import mudfox.repository.UserRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Optional;

/*
@GetMapping = @RequestMapping(method = RequestMethod.GET)
@PostMapping = @RequestMapping(method = RequestMethod.POST)
@PutMapping = @RequestMapping(method = RequestMethod.PUT)
@DeleteMapping = @RequestMapping(method = RequestMethod.DELETE)
 */

// value 属性指定映射路径或URL模板
// 指定请求的实际地址，指定的地址可以是URL模板，正则表达式或路径占位，该属性与path互为别名关系

// @Api(tags = "User Controller")
@RestController    // This means that this class is a Controller
@RequestMapping("/User") // This means URL's start with /demo (after Application path)
public class UserController extends BaseController {

    @Autowired // This means to get the bean called userRepository
    // Which is auto-generated by Spring, we will use it to handle the data
    private UserRepository userRepository;

    public UserController() {
        functions.add("name:name");
        functions.add("find_all");
    }

    @ApiOperation("index")
    @GetMapping("")
    Result index() {
        return show_status();
    }

    @ApiOperation("查询所有API")
    @GetMapping("/show_api")
    Result showApi() {
        return show_api();
    }

    /*
    @GetMapping("/add") // Map ONLY GET Requests, @GetMapping(path= "/add")
    // @ResponseBody // @ResponseBody means the returned String is the response, not a view name
    public Result addNewUser(@RequestParam String name,
                             @RequestParam Integer age,
                             @RequestParam String address) {
        // @RequestParam means it is a parameter from the GET or POST request
        User user = new User();
        user.setName(name);
        user.setAge(age);
        user.setAddress(address);
        User result = userRepository.save(user);

        return new Result(result.getId());
    }

        @GetMapping("/name/{name}")
    public User findUserByName(@PathVariable("name") String name) {
        return userRepository.findByName(name);
    }

    @GetMapping("/find")
    public Iterable<User> getUsers(@RequestParam String name) {
        return userRepository.findByNameContaining(name);
    }
    */

    @ApiOperation("查询所有数据")
    @GetMapping("/find_all")
    Result findAllUsers() {
        // This returns a JSON or XML with the users
        Iterable<User> items = userRepository.findAll();
        Result result = new Result(entry);
        result.put("detail", items);
        return result;
    }

    @ApiOperation("根据ID查询单条数据")
    @GetMapping("/id/{id}")
        // @PathVariable("id")
    Result findUserById(@PathVariable Integer id) {
        Optional<User> item = userRepository.findById(id);
        return new Result(entry, "item", item);
    }

    @ApiOperation("根据name查询数据")
    @GetMapping("/name/{name}")
    // @PathVariable("name")
    public Result findUsers(@PathVariable String name) {
        Iterable<User> items = userRepository.findByNameContaining(name);
        return new Result(entry, "detail", items);
    }

    @ApiOperation("新增数据")
    // @PostMapping(value = "/api")
    @PostMapping("/item")
    Result addItem(@RequestBody User user) {
        User result = userRepository.save(user);
        return new Result(entry, result.getId());
    }

    @ApiOperation("根据ID更新数据")
    @PutMapping("/id/{id}")
    Result setItem(@RequestBody User newUser,
                   @PathVariable Integer id) {
        newUser.setId(id);
        User result = userRepository.save(newUser);
        return new Result(entry, "updated_id", result.getId());
    }

    @ApiOperation("根据ID删除单条数据")
    @DeleteMapping("/id/{id}")
    Result removeItem(@PathVariable Integer id) {
        userRepository.deleteById(id);
        return new Result(entry, "deleted_id", id);
    }
}

/*

@RequestMapping
RequestMapping是一个用来处理请求地址映射的注解，可用于类或方法上。用于类上，表示类中的所有响应请求的方法都是以该地址作为父路径。
RequestMapping注解有六个属性，下面我们把她分成三类进行说明。
1、 value， method；
value：     指定请求的实际地址，指定的地址可以是URI Template 模式；
method：  指定请求的method类型， GET、POST、PUT、DELETE等；

2、 consumes，produces；
consumes： 指定处理请求的提交内容类型（Content-Type），例如application/json, text/html;
produces:  指定返回的内容类型，仅当request请求头中的(Accept)类型中包含该指定类型才返回;

3、 params，headers；
params： 指定request中必须包含某些参数值是，才让该方法处理。
headers： 指定request中必须包含某些指定的header值，才能让该方法处理请求。
 */